package cp3120.view;

import cp3120.dao.DAOFactory;
import cp3120.model.Airport;
import cp3120.model.Flight;
import cp3120.ucm.UC2ALAdapter;
import cp3120.ucm.client.UCDeleteFlight;
import cp3120.ucm.client.UCUpdateFlight;
import cp3120.ucm.client.UCAddFlight;
import generic.swing.List2ComboBoxModelAdapter;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.List;
import javax.swing.ComboBoxModel;
import javax.swing.JFrame;

/**
 * A view of a flight.
 * @author adam
 */
public class FlightView extends JFrame {
    private Flight flight;
    private ComboBoxModel departureModel;
    private ComboBoxModel arrivalModel;

    public FlightView(Flight flight, boolean editable) {
        init();
        initComponents();
        setEditable(editable);
        loadFrom(flight);
        update.addActionListener(new UC2ALAdapter(new UCUpdateFlight(this)));
        delete.addActionListener(new UC2ALAdapter(new UCDeleteFlight(this)));
        create.setVisible(false);
    }

    public FlightView() {
        init();
        initComponents();
        setEditable(true);
        this.flight = new Flight();
        update.setVisible(false);
        delete.setVisible(false);
        buyTicket.setVisible(false);
        create.addActionListener(new UC2ALAdapter(new UCAddFlight(this)));
    }

    public void init() {
        List<Airport> airports = DAOFactory.getFlightDAO().getAirports();
        departureModel = new List2ComboBoxModelAdapter(airports);
        arrivalModel = new List2ComboBoxModelAdapter(airports);
    }

    public void setEditable(boolean editable) {
        arrivalLocation.setEditable(editable);
        arrivalDate.setEditable(editable);
        departureLocation.setEditable(editable);
        departureDate.setEditable(editable);
        seats.setEditable(editable);
        price.setEditable(editable);

        update.setVisible(editable);
    }

    public void loadFrom(Flight flight) {
        this.flight = flight;
        arrivalModel.setSelectedItem(flight.getArrivalLocation());
        String[] arrivalParts = flight.getArrivalTime().split(" ");
        arrivalDate.setText(arrivalParts[0]);
        arrivalTime.setText(arrivalParts[1]);
        departureModel.setSelectedItem(flight.getDepartureLocation());
        String[] departureParts = flight.getDepartureTime().split(" ");
        departureDate.setText(departureParts[0]);
        departureTime.setText(departureParts[1]);
        seats.setText(Integer.toString(flight.getSeats()));
        price.setText(NumberFormat.getCurrencyInstance().format(flight.getPrice()));
    }

    public Flight getFlight() {
        flight.setArrivalLocation((Airport)arrivalLocation.getSelectedItem());
        flight.setArrivalTime(arrivalDate.getText() + " " + arrivalTime.getText());
        flight.setDepartureLocation((Airport)departureLocation.getSelectedItem());
        flight.setDepartureTime(departureDate.getText() + " " + departureTime.getText());
        flight.setSeats(Integer.valueOf(seats.getText()));
        try {
            flight.setPrice(NumberFormat.getCurrencyInstance().parse(price.getText()).floatValue());
        } catch (ParseException ex) {
            // My fucking god! just return zero you tools! shit we arn't playing baseball here.
            flight.setPrice(0.0f);
        }

        return flight;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buyTicket = new javax.swing.JButton();
        update = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        price = new javax.swing.JFormattedTextField();
        seats = new javax.swing.JFormattedTextField();
        departureDate = new javax.swing.JFormattedTextField();
        arrivalDate = new javax.swing.JFormattedTextField();
        departureTime = new javax.swing.JFormattedTextField();
        arrivalTime = new javax.swing.JFormattedTextField();
        arrivalLocation = new javax.swing.JComboBox();
        departureLocation = new javax.swing.JComboBox();
        delete = new javax.swing.JButton();
        create = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Flight");

        buyTicket.setText("Buy Ticket");
        buyTicket.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buyTicketActionPerformed(evt);
            }
        });

        update.setText("Update");

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Departure Location:");

        jLabel2.setText("Departure Time:");

        jLabel3.setText("Arrival Location:");

        jLabel4.setText("Arrival Time:");

        jLabel5.setText("Seats:");

        jLabel6.setText("Price:");

        price.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("¤#,##0.00"))));

        seats.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(java.text.NumberFormat.getIntegerInstance())));

        departureDate.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("yyyy-mm-dd"))));

        arrivalDate.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("yyyy-mm-dd"))));

        departureTime.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("HH:mm:ss"))));

        arrivalTime.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("HH:mm:ss"))));

        arrivalLocation.setModel(departureModel);

        departureLocation.setModel(arrivalModel);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                      .addContainerGap()
                      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(jLabel2)
                                .addComponent(jLabel3)
                                .addComponent(jLabel4)
                                .addComponent(jLabel5)
                                .addComponent(jLabel6))
                      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(price, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                                .addComponent(seats, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                          .addComponent(departureDate, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                          .addComponent(departureTime, javax.swing.GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                          .addComponent(arrivalDate, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                          .addComponent(arrivalTime, javax.swing.GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE))
                                .addComponent(arrivalLocation, 0, 208, Short.MAX_VALUE)
                                .addComponent(departureLocation, 0, 208, Short.MAX_VALUE))
                      .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                      .addContainerGap()
                      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel1)
                                .addComponent(arrivalLocation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2)
                                .addComponent(departureDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(departureTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel3)
                                .addComponent(departureLocation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel4)
                                .addComponent(arrivalDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(arrivalTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel5)
                                .addComponent(seats, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel6)
                                .addComponent(price, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                      .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        delete.setText("Delete");

        create.setText("Create");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                      .addContainerGap()
                      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                          .addComponent(create)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 126, Short.MAX_VALUE)
                                          .addComponent(delete)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                          .addComponent(update)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                          .addComponent(buyTicket)))
                      .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                      .addContainerGap()
                      .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(buyTicket)
                                .addComponent(update)
                                .addComponent(delete)
                                .addComponent(create))
                      .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buyTicketActionPerformed(java.awt.event.ActionEvent evt) { //GEN-FIRST:event_buyTicketActionPerformed
        new TicketView(flight.getTicket()).setVisible(true);
        dispose();
    }//GEN-LAST:event_buyTicketActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFormattedTextField arrivalDate;
    private javax.swing.JComboBox arrivalLocation;
    private javax.swing.JFormattedTextField arrivalTime;
    private javax.swing.JButton buyTicket;
    private javax.swing.JButton create;
    private javax.swing.JButton delete;
    private javax.swing.JFormattedTextField departureDate;
    private javax.swing.JComboBox departureLocation;
    private javax.swing.JFormattedTextField departureTime;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JFormattedTextField price;
    private javax.swing.JFormattedTextField seats;
    private javax.swing.JButton update;
    // End of variables declaration//GEN-END:variables

}
